<?php

namespace App\Http\Controllers\Registry;

use App\Enums\AuthType;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Models\RegistryAuth;

class AuthController extends Controller
{
    /** 查询认证列表 */
    public function listAuths()
    {
        return $this->myAuthQuery()->get()
            ->map(function (RegistryAuth $auth) {
                return $auth->toSimpleArray();
            });
    }

    /** 查询认证详情 */
    public function getAuth($id)
    {
        $auth = $this->myAuthQuery()->find($id);
        if (!$auth) {
            return [];
        }
        $result = [
            'id' => $auth->id,
            'auth_type' => $auth->auth_type->value,
            'name' => $auth->auth_name,
            'address' => $auth->address,
        ];
        if ($auth->auth_type->is(AuthType::PERSONAL())) {
            $result += [
                'card_no' => $auth->id_card_no,
                'card_front_image' => $auth->id_card_image_front,
                'card_back_image' => $auth->id_card_image_back,
                'gender' => $auth->gender,
                'nation' => $auth->nation,
            ];
        }
        if ($auth->auth_type->is(AuthType::COMPANY())) {
            $result += [
                'license_no' => $auth->license_no,
                'license_image' => $auth->license_image,
                'license_expire_date' => $auth->license_expire_date,
            ];
        }

        return $result;
    }

    /** 保存认证用户 */
    public function saveAuth()
    {
        request()->validate([
            'auth_type' => 'required | enum_value:' . AuthType::class,
            'name' => 'required',
            'address' => 'required',
        ]);

        /** @var RegistryAuth $auth */
        $auth = get_entity(RegistryAuth::class);
        if (!$auth) {
            throw new ApiException('无效的认证信息ID');
        }

        $auth->user_id = auth()->id();
        $auth->auth_type = request()->auth_type;
        $auth->auth_name = request()->name;
        $auth->address = request()->address;
        if ($auth->auth_type->is(AuthType::PERSONAL())) {
            request()->validate([
                'card_no' => 'required',
                'gender' => 'required',
                'nation' => 'required',
                'card_front_image' => 'required',
                'card_back_image' => 'required',
            ]);
            $auth->id_card_no = request()->card_no;
            $auth->gender = request()->gender;
            $auth->nation = request()->nation;
            $auth->id_card_image_front = request()->card_front_image;
            $auth->id_card_image_back = request()->card_back_image;
        } else {
            request()->validate([
                'license_no' => 'required',
                'license_image' => 'required',
                'license_expire_date' => 'nullable',
            ]);
            $auth->license_no = request()->license_no;
            $auth->license_image = request()->license_image;
            $auth->license_expire_date = request()->license_expire_date;
        }
        $auth->save();

        return $auth->toSimpleArray();
    }

    /** 删除认证信息 */
    public function deleteAuth($id)
    {
        $this->myAuthQuery()->delete();
    }

    private function myAuthQuery()
    {
        return RegistryAuth::query()->where('user_id', auth()->id());
    }
}
